using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.IO;

namespace Microsoft.SharedSource.SqlServer.MDDE.ManagementStudio
{
    internal class UpgradeTemplateWizardController
    {
        XmlDocument wizardConfigurations;
        string repositoryConnectionString;
        string templateName;

        public XmlDocument WizardConfigurations
        {
            get { return wizardConfigurations; }
        }
        public string RepositoryConnectionString
        {
            get { return repositoryConnectionString; }
        }

        public string TemplateName
        {
            get { return templateName; }
        }

        List<UpgradeTemplateBase> dialogs = new List<UpgradeTemplateBase>();

        int listIndex = -1;

        UpgradeTemplateWelcome welcome;
        UpgradeTemplateSelectLocation selectLocation;
        UpgradeTemplateShowDifferences showDifferences;
        UpgradeTemplateSelectConfigurations selectConfigurations; 
        UpgradeTemplateSelectTemplateInstances selectTemplateInstance;
        UpgradeTemplateSelectPackages selectPackages;
        UpgradeTemplateCompleteWizard completeWizard;
        UpgradeTemplateShowProgress showProgress;

        internal UpgradeTemplateWizardController(string repositoryConnectionString, string templateName)
        {
            this.repositoryConnectionString = repositoryConnectionString;
            this.templateName = templateName;

            // Create the wizard configuration document.
            wizardConfigurations = new XmlDocument();
            UIHelper.AppendChildElement(wizardConfigurations, "UpgradeTemplate",
                new string[]{"ConnectionString",repositoryConnectionString,
                    "Template",templateName});

            // Instantiate the wizard dialogs and add them to the list in the display order.
            welcome = new UpgradeTemplateWelcome(this);
            AddDialog(welcome);
            selectLocation = new UpgradeTemplateSelectLocation(this);
            AddDialog(selectLocation);
            showDifferences = new UpgradeTemplateShowDifferences(this);
            AddDialog(showDifferences);
            selectConfigurations = new UpgradeTemplateSelectConfigurations(this);
            AddDialog(selectConfigurations);
            selectTemplateInstance = new UpgradeTemplateSelectTemplateInstances(this);
            AddDialog(selectTemplateInstance);
            selectPackages = new UpgradeTemplateSelectPackages(this);
            AddDialog(selectPackages);
            completeWizard = new UpgradeTemplateCompleteWizard(this);
            AddDialog(completeWizard);

            // The last form in the flow.
            showProgress = new UpgradeTemplateShowProgress(this);
            AddDialog(showProgress);

            // Initialize the list counter.
            listIndex = 0;
        }

        private void AddDialog(UpgradeTemplateBase dialog)
        {
            // Add a new instance of the dialog to list.
            dialogs.Add(dialog);

            // Wire the event handlers.
            dialog.HelpClick += new EventHandler(this.HelpHandler);

            dialog.BackClick += new EventHandler(this.BackHandler);

            dialog.NextClick += new EventHandler(this.NextHandler);

            dialog.FinishClick += new EventHandler(this.FinishHandler);

            dialog.CancelClick += new EventHandler(this.CancelHandler);
        }

        void FinishHandler(object sender, EventArgs e)
        {
            // Start off with the processing of wizardConfigurations from here.
            NextHandler(sender, e);
        }

        void BackHandler(object sender, EventArgs e)
        {
            // Set the size and location of each of the form to the current one.
            foreach (UpgradeTemplateBase dialog in this.dialogs)
            {
                dialog.Size = dialogs[listIndex].Size;
                dialog.Location = dialogs[listIndex].Location;
            }

            // If there is some non-null dialog previous to the currently showing dialog, 
            // hide the current dialog and display the previous dialog.
            if (listIndex > 0 && dialogs[listIndex] != null && dialogs[listIndex - 1] != null)
            {
                dialogs[listIndex].Hide();
                dialogs[listIndex - 1].Show();
                listIndex--;
            }
        }

        internal void BeginWizardFlow()
        {
            //// Display the first dialog.
            if (dialogs[0] != null)
                dialogs[0].Show();
            // Hack: Comment out the line below while using the wizard in real code.
            //Application.Run(dialogs[0]);
        }

        void NextHandler(object sender, EventArgs e)
        {
            // Set the size and location of each of the form to the current one.
            foreach (UpgradeTemplateBase dialog in this.dialogs)
            {
                dialog.Size = dialogs[listIndex].Size;
                dialog.Location = dialogs[listIndex].Location;
            }

            // If there is some non-null dialog after the currently showing dialog, 
            // hide the current dialog and display the next dialog.
            if (listIndex < dialogs.Count - 1 &&
                dialogs[listIndex] != null &&
                dialogs[listIndex + 1] != null)
            {
                dialogs[listIndex].Hide();
                dialogs[listIndex + 1].Show();
                listIndex++;
            }
        }

        void HelpHandler(object sender, EventArgs e)
        {
            MessageBox.Show("Currently, no help is available.",
                "MDDE Upgrade Template Wizard",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        void CancelHandler(object sender, EventArgs e)
        {
            // Close all the forms.
            foreach (UpgradeTemplateBase dialog in this.dialogs)
                dialog.Close();
        }
    }
}
